package com.right.health.app.ui.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.widget.refresh.OnLastItemVisibleListener;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.FamilyResponse;
import com.right.health.app.bean.ReviewReminderInfo;
import com.right.health.app.bean.ReviewReminderResponse;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.RemindApi;
import com.right.health.app.ui.me.adapter.ReviewReminderAdapter;

import java.util.ArrayList;
import java.util.List;

import static com.right.health.app.constants.BroadCastCode.REFRESH_CHOOSE_FAMILY;

/*
 * Created by hopeliao on 2019-09-29
 * 复诊提醒
 *
 */
public class ReviewReminderActivity extends BaseActivity implements View.OnClickListener, OnLastItemVisibleListener,
        SwipeRefreshLayout.OnRefreshListener, OnHttpListener {

    private static final int HTTP_CODE_REFRESH = 1;
    private static final int HTTP_CODE_LOAD_MORE = 2;

    private static final int REQUEST_CODE_FAMILY = 1;

    private ReviewReminderAdapter mAdapter;

    private TextView mNameTv;

    private SwipeListRefreshLayout mRefreshView;

    private ListView listView;

    private int mPageNo;

    private RemindApi mRemindApi;

    private List<ReviewReminderInfo> mDataSource = new ArrayList<>();

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.review_reminder);
    }

    @Override
    public void findView() {
        mRefreshView = (SwipeListRefreshLayout) findViewById(R.id.refresh_view);
        mRefreshView.setLastItemVisibleListener(this);
        mRefreshView.setOnRefreshListener(this);

        mNameTv = (TextView) findViewById(R.id.tv_name);

        findViewById(R.id.ib_add).setOnClickListener(this);
        findViewById(R.id.tv_choose_family).setOnClickListener(this);

        listView = (ListView) findViewById(R.id.listview);
        listView.setEmptyView(findViewById(R.id.empty_view));
        mAdapter = new ReviewReminderAdapter(this);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ReviewReminderInfo info = (ReviewReminderInfo)parent.getItemAtPosition(position);
                if(info != null) {

                    Bundle bundle = new Bundle();
                    bundle.putParcelable(BroadCastKey.DATA, info);
                    startIntent(ReviewReminderDetailActivity.class, bundle);
                }

            }
        });
        findViewById(R.id.tv_goto_add).setOnClickListener(this);
    }

    @Override
    public void initialize() {
//        mAdapter.setDataSource(DataCommon.getTestDataSource());
        mNameTv.setText(App.getInstance().getCurrentMember().name);

        mRemindApi = new RemindApi();
        mRemindApi.setListener(this);

        onReceiveBroadcast(BroadCastCode.RREFRESH_DRUGREINDE, null);
    }

    @Override
    public void onReceiveBroadcast(int intent, Bundle bundle) {
        super.onReceiveBroadcast(intent, bundle);
        if (intent == BroadCastCode.RREFRESH_DRUGREINDE) {
            mRefreshView.setRefreshing(true);
            onRefresh();
        } else if(intent == REFRESH_CHOOSE_FAMILY) {
            mNameTv.setText(App.getInstance().getCurrentMember().name);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_FAMILY) {
                Bundle bundle = data.getExtras();
                if (bundle != null) {
                    FamilyResponse.FamilyInfo mFamilyInfo = bundle.getParcelable(BroadCastKey.DATA);
                    if (mFamilyInfo != null) {
                        mNameTv.setText(mFamilyInfo.membername);
                        onReceiveBroadcast(BroadCastCode.RREFRESH_DRUGREINDE, null);
                    }
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.ib_add || v.getId() == R.id.tv_goto_add) {
            startIntent(ReviewReminderAddActivity.class);
        }else if(v.getId() == R.id.tv_choose_family) {
            FamilyActivity.makeActivity(this, true, REQUEST_CODE_FAMILY);
        }
    }

    @Override
    public void onRefresh() {
        mPageNo = 1;
        mRemindApi.addRequestCode(HTTP_CODE_REFRESH);
        mRemindApi.getReviewReminder(App.getInstance().getCurrentMember().id, mPageNo);
    }

    @Override
    public void onLastItemVisible() {
        mPageNo += 1;
        mRemindApi.addRequestCode(HTTP_CODE_LOAD_MORE);
        mRemindApi.getReviewReminder(App.getInstance().getCurrentMember().id, mPageNo);
    }

    @Override
    public void onResponse(Request request, Response response) {
        mRefreshView.onRefreshComplete();
        if(response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if(response.targetData instanceof ReviewReminderResponse) {
                ReviewReminderResponse reviewReminderResponse = response.cast(ReviewReminderResponse.class);
                if (request.requestCode == HTTP_CODE_REFRESH) {
                    mDataSource.clear();
                    listView.setEmptyView(findViewById(R.id.empty_view));
                }
                if (reviewReminderResponse.page != null) {
                    mRefreshView.setCanLoadMore(!reviewReminderResponse.page.lastPage);
                }
                mDataSource.addAll(reviewReminderResponse.visitagains);
                mAdapter.setDataSource(mDataSource);
            }
        }
    }
}
